package com.likeyoudo.views.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.blankj.utilcode.util.ToastUtils;
import com.google.android.material.tabs.TabLayoutMediator;
import com.likeyoudo.common.BaseActivity;
import com.likeyoudo.common.msg.MsgEvent;
import com.likeyoudo.views.R;
import com.likeyoudo.views.adapter.FilePreviewFragmentAdapter;
import com.likeyoudo.views.constants.xg.ApprovalConstants;
import com.likeyoudo.views.databinding.ActivityFilePreviewBinding;
import com.likeyoudo.views.fragment.FilePreviewFragment;
import com.likeyoudo.views.utils.HttpRequest;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LikeYouDo
 * @date 2024/1/9 15:08
 */
public class FilePreViewActivity extends BaseActivity {

    ActivityFilePreviewBinding binding;
    List<FilePreviewFragment> list = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_file_preview;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityFilePreviewBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initView();
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initView() {

        setImmersiveWindows();
        showBackButton();
        setTitle("文件预览");
        showMenu(R.menu.refresh_menu, new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int itemId = item.getItemId();
                if (itemId == R.id.refresh) {
                    FilePreviewFragment currentFragment = (FilePreviewFragment) ((FilePreviewFragmentAdapter) binding.vp2Image.getAdapter()).createFragment(binding.vp2Image.getCurrentItem());
                    currentFragment.reloadPdf();
                }
                return true;
            }
        });

        // 获取Intent实例
        Intent intent = getIntent();

        // 获取字符串数据
        String files = intent.getStringExtra(ApprovalConstants.FILES_KEY);
        if (files != null) {
            // 使用传递过来的字符串数据
            String[] split = files.split(",");
            // 创建Fragment
            for (String s : split) {
                list.add(new FilePreviewFragment(HttpRequest.HOST + s));
            }
            FilePreviewFragmentAdapter adapter = new FilePreviewFragmentAdapter(getSupportFragmentManager(), getLifecycle(), list);
            binding.vp2Image.setAdapter(adapter);
            // 禁止左右滑动
            binding.vp2Image.setUserInputEnabled(false);

            // TabLayout与ViewPager2联动
            new TabLayoutMediator(binding.tab, binding.vp2Image,
                    (tab, position) -> {
                        tab.setText("文件 " + (position + 1)); // 设置标签文本
                    }
            ).attach();
        } else {
            ToastUtils.showShort("文件预览失败");
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MsgEvent event) {

    }
}
